<script>
import storage from '@/utils/storage.js'
import {
  SYSTEM_CLIENT,
  CHANNEL_ID
} from '@/store/mutation-types.js'
import { headerParams } from '@/config/headerParams.js'

export default {
  onLaunch(options) {
    const { query } = options
    const { channel_id } = query
		
    // 设置渠道ID到本地数据中
    const oldChannelId = storage.get(CHANNEL_ID)
    const defaultChannelId = headerParams.pid
    if(oldChannelId && oldChannelId > 6000) {
      // 数据库渠道ID从6000开始自动增长。已有推广渠道，保持推广渠道
      console.log('已有推广渠道，保持推广渠道')
    } else {
      // 有新渠道并设置，否则设置默认渠道
      const access = channel_id || defaultChannelId
      storage.set(CHANNEL_ID, access)
    }
		
    // 设置系统标识
    this.setSystemInfoSync()
    // 加载系统信息，安卓或iOS
    this.$store.dispatch('SystemInfo')

    //存在则关闭启动页进入首页	
    // #ifdef APP-PLUS
    plus.navigator.closeSplashscreen()
    // #endif
  },
  onShow() {},
  onHide() {},
  methods: {
    // 设置系统标识  0=未知 1=安卓 2=ios 3=小程序安卓 4=小程序ios
    setSystemInfoSync() {
      const systemInfoSync = uni.getSystemInfoSync()
      const { osName } = systemInfoSync
      if (osName === 'ios') storage.set(SYSTEM_CLIENT, 4)
      else if (osName === 'android') storage.set(SYSTEM_CLIENT, 3)
      else storage.set(SYSTEM_CLIENT, 0)
    }
  },
  onPageNotFound: [
    function(res) {
      // 跳转到404页面
      uni.redirectTo({ url: 'pages/error/404', })
    },
  ]
}
</script>

<style lang="scss">
	@import "@/uni_modules/uview-ui/index.scss";
	@import "@/static/style.scss";
	@import "@/static/copy-uview-theme.scss";
	@import "@/static/common.scss";
</style>
